#ifndef _SNAKE_H_
#define _SNAKE_H_
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<stdbool.h>
#include<termios.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<signal.h>

#define WIDE 60
#define HIGH 20
#define MAX_OBSTACLES 50

struct BODY{
    int X;
    int Y;
};

// 定义蛇对象
extern struct SNAKE{
    struct BODY body[WIDE*HIGH];
    int size;
}snake;

// 定义食物对象
extern struct FOOD{
    int X;
    int Y;
}food;

// 初始分数
extern int score;

typedef enum{
    DIR_UP,DIR_DOWN,DIR_LEFT,DIR_RIGHT
}Direction;

struct GameState{
    Direction currentDir;
    Direction lastDir;
    bool gameOver;
    int score;
    int speed;
    int level;
};

extern struct GameState game;

typedef struct{
    int X;
    int Y;
}Obstacle;

extern Obstacle obstacles[MAX_OBSTACLES];

extern int kx;
extern int ky;
extern int lastX;
extern int lastY;
extern int sleepSecond;
extern bool isPaused;
extern int obstacleCount;

typedef struct UserNode {
    char id[50];  
    int score;
    struct UserNode* next;
}UserNode;

extern UserNode *head;
extern UserNode* currentUser;

// 函数声明
void showPauseScreen();
void resumeGameScreen();
void initSnake();
void initFood();
void initUI();
void initObstacle();
void updateLevel();
void handleInput();
void moveSnake();
void checkCrush();
void playGame();
void initWall();
void showScore();
void showLevelInfo();

void initUserSystem();
UserNode* createUserNode(const char* id);
UserNode* findUser(const char* id);
void initUser();
void updateUserScore(int newScore);
void saveUserScores();
void loadUserScores();
void showScoreRanking();
void gameOver();
void initGame();

// 控制台相关函数声明
void hideCursor();
void showCursor();
void setCursorPosition(int x, int y);
void clearScreen();
int kbhit();
int getch();

#endif